package com.self.serviceedu.controller;


import com.self.commonutils.R;
import com.self.serviceedu.entity.EduChapter;
import com.self.serviceedu.entity.chapter.ChapterVo;
import com.self.serviceedu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@Api(description = "课程章节小节信息")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //课程大纲列表
    @ApiOperation(value = "根据课程Id获取章节小节")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(
            @ApiParam(name = "courseId",value = "课程Id",required = true)
            @PathVariable String courseId){

        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节
    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(
            @ApiParam(name = "eduChapter",value = "课程章节",required = true)
            @RequestBody EduChapter eduChapter){

        eduChapterService.save(eduChapter);
        return R.ok();
    }

    //根据章节Id查询
    @GetMapping("getChapterInfo/{chapterId}")
    @ApiOperation(value = "根据章节Id查询")
    public R getChapterInfo(
            @ApiParam(name = "chapterId",value = "章节ID",required = true)
            @PathVariable("chapterId") String chapterId){

        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);
    }

    //修改章节
    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(
            @ApiParam(name = "eduChapter",value = "课程章节对象",required = true)
            @RequestBody EduChapter eduChapter) {

        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节
    @ApiOperation(value = "删除章节")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(
            @ApiParam(name = "chapterId",value = "课程ID",required = true)
            @PathVariable("chapterId") String chapterId){

        boolean flag = eduChapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }

    }


}

